# coding: utf-8
from subprocess import run

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QHBoxLayout
from qfluentwidgets import TransparentToolButton, FluentIcon as FIF

from common import cfg, SingleIcon
from components import CustomSimpleCardWidget, SelectLineEdit, ToolTipButton, ListWidget, Alert
from .base import PluginBase


def merge_bili_videos(
        video_path: str,
        audio_path: str,
        output_path: str
):
    cmd = f'{cfg.get(cfg.ffmpegPath)} -loglevel quiet -i {video_path} -i {audio_path} -c copy -y {output_path}'
    run(cmd)


class BiliMergeCard(CustomSimpleCardWidget):
    deleteSignal = Signal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.videoEdit = SelectLineEdit(self)
        self.audioEdit = SelectLineEdit(self)
        self.outputEdit = SelectLineEdit(self)
        self.deleteButton = TransparentToolButton(FIF.CLOSE, self)

        self.videoEdit.setPlaceholderText("视频文件路径")
        self.audioEdit.setPlaceholderText("音频文件路径")
        self.outputEdit.setPlaceholderText("输出文件路径")

        self.videoEdit.setType("getFile")
        self.audioEdit.setType("getFile")
        self.outputEdit.setType("setFile")

        self.videoEdit.setFilter("视频文件 (*)")
        self.audioEdit.setFilter("音频文件 (*)")
        self.outputEdit.setFilter("视频文件 (*.mp4 *.mkv)")

        self.deleteButton.clicked.connect(lambda _: self.deleteSignal.emit(self))

        self.setFixedHeight(50)
        self.horizontalLayout.addWidget(self.videoEdit)
        self.horizontalLayout.addWidget(self.audioEdit)
        self.horizontalLayout.addWidget(self.outputEdit)
        self.horizontalLayout.addWidget(self.deleteButton)

    def params(self):
        video_path = self.videoEdit.text()
        audio_path = self.audioEdit.text()
        output_path = self.outputEdit.text()
        if not video_path or not audio_path or not output_path:
            Alert.warning("请选择视频文件、音频文件和输出文件路径")
            return None
        return {
            "video_path": video_path,
            "audio_path": audio_path,
            "output_path": output_path
        }


class BiliMergeWidget(PluginBase):
    NAME = "哔哩哔哩音视频合并"
    DESCRIPTION = "将视频和音频合并为一个视频文件"
    ICON = SingleIcon.BILI

    def __init__(self, parent=None):
        super().__init__(parent)
        self.cardList = []
        self.addBtn = ToolTipButton.transparentToolButton(self, FIF.ADD, '添加一行')
        self.startBtn = ToolTipButton.transparentToolButton(self, FIF.PLAY, '开始合并')
        self.listWidget = ListWidget(self)

        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.addBtn)
        self.horizontalLayout.addWidget(self.startBtn)
        self.addWidget(self.listWidget)

        self.addBtn.clicked.connect(self.addRow)
        self.startBtn.clicked.connect(self.startMerge)

    def addRow(self):
        card = BiliMergeCard(self)
        card.deleteSignal.connect(self.removeRow)
        self.cardList.append(card)
        self.listWidget.addWidget(card)

    def removeRow(self, card: BiliMergeCard):
        self.listWidget.removeWidget(card)
        self.cardList.remove(card)

    def startMerge(self):
        for card in self.cardList:
            params = card.params()
            if params:
                self.load(merge_bili_videos, **params)
